<?php

/**
 * nodo1 actions.
 *
 * @package    sensores
 * @subpackage nodo1
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class nodo1Actions extends sfActions
{
  public function executeMenu_operaciones(sfWebRequest $request){}
  public function executeMenu_graficos(sfWebRequest $request){}
  public function executeBuscar_editar(sfWebRequest $request){}  
  public function executeEliminar_total(sfWebRequest $request){}
  public function executeIndex(sfWebRequest $request){}
  /****************************************************************************************************************/
  public function executeTemperatura(sfWebRequest $request){}
  public function executeDatostemperatura(sfWebRequest $request)
  {
        $categorias = array('nombre');
	$temperatura1 = array('Temperatura mota1');
        $temperatura2 = array('Temperatura mota2');
        
        $query = "SELECT nombre FROM red_sensores WHERE ((nombre = 'mota_1') OR (nombre = 'mota_2'));"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        $c = '';
        foreach($resultado as $item):
            $c .= $item[0].' ';                        
        endforeach;
        $categorias[] = $c;            

        $query = "SELECT nombre, temperatura FROM red_sensores WHERE (nombre = 'mota_1') UNION SELECT nombre, temperatura FROM red_sensores WHERE (nombre = 'mota_2');"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_1'):
                $temperatura1[] = (double) $elemento[1];                
            elseif ($elemento[0] == 'mota_2') :            
                $temperatura2[] = (double) $elemento[1];                
            endif;
        endforeach;        
        echo json_encode( array($categorias, $temperatura1, $temperatura2) );
  }
  /***************************************************************************************************************/
  public function executeHumedad(sfWebRequest $request){}
  public function executeDatoshumedad(sfWebRequest $request)
  {
      $categorias = array('nombre');
	$humedad1 = array('Humedad mota1');
        $humedad2 = array('Humedad mota2');
        
        $query = "SELECT nombre FROM red_sensores WHERE ((nombre = 'mota_1') OR (nombre = 'mota_2'));"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        $c = '';
        foreach($resultado as $item):
            $c .= $item[0].' ';                        
        endforeach;
        $categorias[] = $c;            
        
        $query = "SELECT nombre, humedad FROM red_sensores WHERE (nombre = 'mota_1') UNION SELECT nombre, humedad FROM red_sensores WHERE (nombre = 'mota_2');"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_1'):
                $humedad1[] = (double) $elemento[1];                
            elseif ($elemento[0] == 'mota_2') :            
                $humedad2[] = (double) $elemento[1];                
            endif;
        endforeach;
        
        echo json_encode( array($categorias, $humedad1, $humedad2) );
  }
  public function executeLuz(sfWebRequest $request){}
  public function executeDatosluz(sfWebRequest $request)
  {
      $categorias = array('nombre');
	$luz1 = array('Luz Mota1');
        $luz2 = array('Luz Mota2');
        
        $query = "SELECT nombre FROM red_sensores WHERE ((nombre = 'mota_1') OR (nombre = 'mota_2'));"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        $c = '';
        foreach($resultado as $item):
            $c .= $item[0].' ';                        
        endforeach;
        $categorias[] = $c;            
        
        $query = "SELECT nombre, luz FROM red_sensores WHERE (nombre = 'mota_1') UNION SELECT nombre, luz FROM red_sensores WHERE (nombre = 'mota_2');"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_1'):
                $luz1[] = (double) $elemento[1];                
            elseif ($elemento[0] == 'mota_2') :            
                $luz2[] = (double) $elemento[1];                
            endif;
        endforeach;
        
        echo json_encode( array($categorias, $luz1, $luz2) );
  }
  public function executeVoltaje(sfWebRequest $request){}
  public function executeDatosvoltaje(sfWebRequest $request)
  {
      $categorias = array('nombre');
	$voltaje1 = array('Voltaje Mota1');
        $voltaje2 = array('Voltaje Mota2');
        
        $query = "SELECT nombre FROM red_sensores WHERE ((nombre = 'mota_1') OR (nombre = 'mota_2'));"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        $c = '';
        foreach($resultado as $item):
            $c .= $item[0].' ';                        
        endforeach;
        $categorias[] = $c;            
        
        $query = "SELECT nombre, voltaje FROM red_sensores WHERE (nombre = 'mota_1') UNION SELECT nombre, voltaje FROM red_sensores WHERE (nombre = 'mota_2');"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_1'):
                $voltaje1[] = (double) $elemento[1];                
            elseif ($elemento[0] == 'mota_2') :            
                $voltaje2[] = (double) $elemento[1];                
            endif;
        endforeach;
        
        echo json_encode( array($categorias, $voltaje1, $voltaje2) );
  }
  /***************************************************************************************************************/
  public function executeDashboard(sfWebRequest $request){}
  public function executeDatosdashboard(sfWebRequest $request)
  {        
        $categorias = array('nombre');
	$valor1 = array('nombre');
	$valor2 = array('temperatura');        
        $valor3 = array('humedad');
        $valor4 = array('luz');
        $valor5 = array('cantidad');
        
        $query = "SELECT nombre FROM nodo1 WHERE (nombre = 'mota_1') OR (nombre = 'mota_2') GROUP BY(nombre);"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
                
        foreach($resultado as $item):
            $categorias[] = $item[0];
        endforeach;

        $query1 = "SELECT nombre, AVG(temperatura) as temperatura, AVG(humedad)as humedad, AVG(luz) as luz, count(*) as cantidad_registros FROM nodo1 WHERE (MINUTE(hora) >= 1) AND (MINUTE(hora) <= 10) GROUP BY nombre;";
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query1);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_1'):
                
                $valor2[] = (int) $elemento[1];
                $valor3[] = (int) $elemento[2];
               $valor4[] = (int) $elemento[3];
//                $valor5[] = (int) $elemento[4];
            
            elseif($elemento[0] == 'mota_2'):
                
                $valor2[] = (int) $elemento[1];
                $valor3[] = (int) $elemento[2];
                $valor4[] = (int) $elemento[3];
//                $valor5[] = (int) $elemento[4];
                
             endif;            
        endforeach;        
        echo json_encode( array($categorias, $valor2, $valor3, $valor4/*, $valor5*/) );                    
  }
    
  
  public function executeTemperatura_area(sfWebRequest $request){}
  public function executeDatostemperatura_area(sfWebRequest $request)
  {
        $categorias = array('hora', 'mota_1', 'mota_2');
	$mota1 = array();
        $mota2 = array(); 
        $total = array();        

        $total[] = $categorias;
       
        $query = "SELECT hora, nombre, temperatura FROM nodo1 WHERE ((MINUTE(hora) >= 0) AND (MINUTE(hora)<= 10)) UNION SELECT hora, nombre, temperatura FROM nodo2 WHERE ((MINUTE(hora) >= 0) AND (MINUTE(hora)<= 10)) ORDER BY MINUTE(hora) ASC;";                       
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();        
        foreach($resultado as $elemento):           
            if($elemento[1] == 'mota_1'):
                $mota1[] =  $elemento[0];
                $mota1[] = (int) $elemento[2];               
                $total[] = $mota1;
                $mota1 = array();
                 //$mota2[] =  $elemento[0];
                ///$mota2[] = (int) $elemento[2];                
                endif;
                
        endforeach;        
        echo json_encode( $total);
  } 
  
  /****************************************************************************************************************/
  public function executeEliminar(sfWebRequest $request)
  {
      Doctrine::getTable('Nodo1')->createQuery('a')->delete();
      $this->redirect('nodo1/listado');
  }
  public function executeListado(sfWebRequest $request)
  {
    //$this->nodo1s = Doctrine_Core::getTable('Nodo1')->createQuery('a')->execute();
        $this->nodos = new sfDoctrinePager('Nodo1', 10);        
        $this->nodos->setQuery(Doctrine::getTable('Nodo1')->createQuery('a'));
        $this->nodos->setPage($request->getParameter('pagina', 1));
        $this->nodos->init();
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->nodo = Doctrine_Core::getTable('Nodo1')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->nodo);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new Nodo1Form();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new Nodo1Form();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($nodo1 = Doctrine_Core::getTable('Nodo1')->find(array($request->getParameter('id'))), sprintf('Object nodo1 does not exist (%s).', $request->getParameter('id')));
    $this->form = new Nodo1Form($nodo1);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($nodo1 = Doctrine_Core::getTable('Nodo1')->find(array($request->getParameter('id'))), sprintf('Object nodo1 does not exist (%s).', $request->getParameter('id')));
    $this->form = new Nodo1Form($nodo1);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($nodo1 = Doctrine_Core::getTable('Nodo1')->find(array($request->getParameter('id'))), sprintf('Object nodo1 does not exist (%s).', $request->getParameter('id')));
    $nodo1->delete();

    $this->redirect('nodo1/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $nodo1 = $form->save();

      $this->redirect('nodo1/edit?id='.$nodo1->getId());
    }
  }
}
